from pymongo import MongoClient

from apps.zhcxkj_center_data.prod.views.get_active_source_id import Common
from common.setting import ensure_path_sep
from third_services.aliexpress.api.api import AliexpressApi
from utils.mongodb_control.batch_by_mongodb import MongoDBByBatch
from utils.mysqlUtils.db_pool import Database
from utils.mysqlUtils.mongodb_utils import MongoDBLink
from utils.otherutils.MyThreadPool import MyThreadPool


class TestUpgradeTemplate:

    def __init__(self,mongo_name,sql):
        self.sql = sql
        self.mongo_name = mongo_name

        self.mysql_client = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"),
                                                          "test_jdw_database")
        self.mongo_client = MongoDBLink(
            uri='mongodb://admin:Zhcx?2021@10.158.158.29:27017/?authSource=admin',
            dbname='test_zhcxkj_java_center', coll_name={self.mongo_name})

    def filter_result(self,source_id):
        index = 0
        finally_pf_item_id_set = set()
        while True:
            get_actual_value_sql = self.sql
            actual_value = self.zhcxkj_center_listing_comprehensive_db.fetch_all_tuple(get_actual_value_sql, (index,source_id))
            if not len(actual_value):
                break
            index = actual_value[-1][0]
            if self.get_intersect_set_by_listing_model(actual_value,source_id) is not None:
                finally_pf_item_id_set |= self.get_intersect_set_by_listing_model(actual_value,source_id)
            else:
                pass
            return finally_pf_item_id_set

    def get_intersect_set_by_listing_model(self,actual_value,source_id):
        pf_item_id_list = list(map(lambda x:str(x[3]),actual_value))

        filters_ObjectId_min = {
            "$and": [
                {"sourceId": source_id},
                {"pfListingId": {"$in": pf_item_id_list}},
            ]
        }
        projection1 = {"_id": 1, "pfListingId": 1}
        res = self.t_sku_strategy_relation_aliexpress.select(filters_ObjectId_min,projection1)
        if not len(res):
            return
        pfListingId_set = set(map(lambda x:x.get('pfListingId'),res))
        return pfListingId_set


if __name__ == '__main__':
    print(TestUpgradeTemplate().filter_result(5204))